package com.yuelu.community.community.controller;

import com.yuelu.community.community.entity.DiscussPost;
import com.yuelu.community.community.entity.Page;
import com.yuelu.community.community.entity.User;
import com.yuelu.community.community.service.DiscussPostService;
import com.yuelu.community.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: HomeController
 * Description:
 *
 * @Author 文勇
 * @Create 2024/8/12 21:48
 * @Version 3.0
 */
@Controller
public class HomeController {
    @Autowired
    public DiscussPostService discussPostService;
    @Autowired
    public UserService userService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String getIndexPage(Model model, Page page){
        page.setRows(discussPostService.findDiscussPostRows(0));
        page.setPath("/index");
        List<DiscussPost> list= discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());
        List<Map<String,Object>> discussPosts = new ArrayList<>();
        if(list!=null){
            for(DiscussPost post:list){
                HashMap<String,Object> map = new HashMap<>();
                map.put("post",post);
                User user = userService.findUserById(post.getUserId());
                map.put("user",user);
                discussPosts.add(map);
            }
            model.addAttribute("discussPosts",discussPosts);
        }
        return  "/index";
    }

}
